package jpa;

import java.util.List;
import java.util.Random;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class StatelessBean
 */
@Stateless
@EJB(name="StatelessBean", beanInterface=IStatelessBean.class)
public class StatelessBean implements IStatelessBean 
{
	@PersistenceContext
	EntityManager em;
	
    /**
     * Default constructor. 
     */
    public StatelessBean()
    {
        
    }
    
    @Override
    public List<Cache> getAllCaches()
    {	
    	Query q = em.createQuery("SELECT c FROM Cache c");
    	List<Cache> caches = q.getResultList();
    	return caches;
    }
    
    //This needs to be worked on I think...
    public List<Cache> getRandomCache()
    {
    	Random r = new Random();   	
    	
    	Query q = em.createQuery("SELECT c FROM Cache c");
       	List<Cache> random = (List<Cache>) q.getParameter(5);   	       
       	
		return random;
	}

	@Override
	public List<Member> getAllMembers() {
		
		Query q = em.createQuery("SELECT m FROM Member m");
		List<Member> members = q.getResultList();
		return members;
	}

	@Override
	public List<Header> getAllHeaders() {
		Query q = em.createQuery("SELECT h FROM Header h");
		List<Header> headers = q.getResultList();
		return headers;
	}
	
	@Override
	public Cache getCache(String s)
	{
		
		List<Cache> cache;
		Query q = em.createQuery("SELECT c FROM Cache c WHERE c.cacheId = '" + s + "'");
		cache = q.getResultList();
		return cache.get(0);
	}
	public void approveCache(int id)
	{
		Cache c;
		Query q = em.createQuery("SELECT c FROM Cache c WHERE c.cacheId = '" + id + "'");
		c = (Cache) q.getSingleResult();
		c.setCacheApproved(1);
		em.merge(c);
	}
	public void deleteCache(int id)
	{
		//Does not actually delete cache from database, but instead sets the CacheApproved to 2, which will not show
		//in any listing, since we grab either ones set to 0 for not approved, or 1 for those that are approved.
		Cache c;
		Query q = em.createQuery("SELECT c FROM Cache c WHERE c.cacheId = '" + id + "'");
		c = (Cache) q.getSingleResult();
		c.setCacheApproved(2);
		em.merge(c);
	}
	public void deleteMember(int id)
	{
		Member m;
		Query q = em.createQuery("SELECT m FROM Member m WHERE m.memId = '" + id + "'");
		m = (Member) q.getSingleResult();
		m.setMemAdmin(2);
		em.merge(m);
	}
	public void createCache(String name, String desc, double lat, double lon, Member m)
	{
		//System.out.println(file);

		Cache c = new Cache();
		c.setMember(m);
		c.setCacheName(name);
		c.setCacheDescription(desc);
		c.setCacheLatitude(lat);
		c.setCacheLongitude(lon);
		em.persist(c);
	}
	
	
	public void addPicToCache(String picUrl, int id)
	{		
		Query q = em.createQuery("SELECT c FROM Cache c WHERE c.cacheId = " + id);
		Cache c = (Cache) q.getSingleResult();
		c.setCachePicurl("/images/"+picUrl);
		em.persist(c);		
	}
	
	public void registerUser(String name, String password, String conPass, String email)
	{
        System.out.println("REGISTER TEST");
        
		Member m = new Member();
		m.setMemName(name);
		m.setMemPass(password);
		m.setMemEmail(email);	
		m.setMemAdmin(0);
		em.persist(m);
	}

	@Override
	public int getCacheID(String name)
	{
		Cache c = new Cache();
		Query q = em.createQuery("SELECT c FROM Cache c WHERE c.cacheName = '" + name + "'");
		c = (Cache) q.getSingleResult();
		
		return c.getCacheId();
	}
	@Override
	public int getHeaderID(String name)
	{
		Header h = new Header();
		Query q = em.createQuery("SELECT h FROM Header h WHERE h.headTitle = '" + name + "'");
		h = (Header) q.getSingleResult();
		
		return h.getHeadId();
	}

	@Override
	public void editCache(int id, String name, String desc, Double lat,
			Double lon) 
	{
		Query q = em.createQuery("SELECT c FROM Cache c WHERE c.cacheId = " + id);
		Cache c = (Cache) q.getSingleResult();
		
		c.setCacheName(name);
		c.setCacheDescription(desc);
		c.setCacheLatitude(lat);
		c.setCacheLongitude(lon);
		em.persist(c);
		
	}
	public void addHeader(String cacheID, String headerName, String headerDesc)
	{
		Cache c = getCache(cacheID);
		Header h = new Header();
		h.setHeadTitle(headerName);
		h.setHeadDescription(headerDesc);
		h.setCache(c);
		em.persist(h);
	}

	@Override
	public void addPicToHeader(String picUrl, int id) 
	{
		Query q = em.createQuery("SELECT h FROM Header h WHERE h.headId = " + id);
		Header h = (Header) q.getSingleResult();
		h.setHeadPicurl("/images/"+picUrl);
		em.persist(h);
	}
	public List<Header> getAllHeadersFromCache(int cacheID)
	{
		Query q = em.createQuery("SELECT h FROM Header h WHERE h.cache.cacheId = " + cacheID);
		
		@SuppressWarnings("unchecked")
		List<Header> h = q.getResultList();
		
		return h;
	}

	@Override
	public Header getHeader(String s) 
	{
		Query q = em.createQuery("SELECT h FROM Header h WHERE h.headId = " + s);
		Header h = (Header) q.getSingleResult();
		return h;
	}

	@Override
	public void editHeader(int id, String name, String desc) 
	{
		Query q = em.createQuery("SELECT h FROM Header h WHERE h.headId =" + id);
		Header h = (Header) q.getSingleResult();
		h.setHeadTitle(name);
		h.setHeadDescription(desc);
		em.persist(h);
		
	}


}
